package iv1201project.extras;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import iv1201project.model.Logthis;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * A class containing helper-methods that doesn't deserve their own classes.
 */
public class Helpers implements Serializable {

    private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
            Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.NORMAL, BaseColor.RED);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);

    public Helpers() {
    }

    /**
     * Converts a password and returns a SHA256-hashed string.
     * @param password The password to convert.
     * @return The converted password.
     * @throws NoSuchAlgorithmException Thrown when the specified algorithm is unavailable.
     */
    @Logthis
    public String toSha256(String password) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(password.getBytes());

        byte byteData[] = md.digest();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }

    /**
     * Converts a password and returns a MD5-hashed string.
     * @param password The password to convert.
     * @return The converted password.
     * @throws NoSuchAlgorithmException Thrown when the specified algorithm is unavailable.
     */
    @Logthis
    public String toMD5(String filename) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(filename.getBytes());

        byte byteData[] = md.digest();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }

    /**
     * Creates a pdf.
     * @param filename The filename for the PDF.
     * @param data The data to be written in the PDF.
     * @throws DocumentException Thrown when PDF document fails to generate.
     * @throws FileNotFoundException Thrown when the file is not found.
     */
    @Logthis
    public void createPDF(String filename, String data) throws DocumentException, FileNotFoundException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        addMetaData(document);
        addTitlePage(document, data);
        document.close();
    }

    private void addMetaData(Document document) {
        document.addTitle("Applicant report");
        document.addSubject("Applicant Report");
        document.addKeywords("Java, PDF");
        document.addAuthor("IV1201");
        document.addCreator("IV1201");
    }

    private void addTitlePage(Document document, String data) throws DocumentException {
        Paragraph preface = new Paragraph();
        addEmptyLine(preface, 1);
        preface.add(new Paragraph("Applicant Report", catFont));
        addEmptyLine(preface, 1);
        preface.add(new Paragraph(
                "Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(preface, 2);
        preface.add(new Paragraph(data, smallBold));
        addEmptyLine(preface, 8);
        preface.add(new Paragraph("Important stuff, ahoy!", redFont));
        document.add(preface);
    }

    private void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }
}
